var dataJson = [];
var ind = null;
$(function(){
	$("#addclothingType,#editclothingType").combobox({
		data:clothingType,
		valueField:'id',  
		textField:'text',
		editable:false
	})
	$("#clothingType").combobox({
		data:clothingType,
		valueField:'id',  
		textField:'text',
	})
	$("#addstatus,#editstatus").combobox({
		data:leaseType,
		valueField:'id',  
		textField:'text',
		editable:false
	})
	loadData();
	$("#restoreDatagrid").datagrid({
		data:dataJson,
	    fit:true,
	    toolbar:'#restoreTolbar',
	    fitColumns:true,
	    rownumbers:true,
	    singleSelect:true,
	    columns:[[  
	        {field:'leaseId',hidden:true,title:'租赁ID',width:100,align:"center"},
	        {field:'userName',title:'客户名称',width:100,align:"center"}, 
	        {field:'idCard',title:'身份证',width:100,align:"center"}, 
	        {field:'clothingName',title:'服装名称',width:100,align:"center"}, 
	        {field:'clothingType',title:'服装类型',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return typeToName(value,clothingType);
	        	}	}, 
	        {field:'clothingRent',title:'服装租金',width:100,align:"center"}, 
	        {field:'foregift',title:'押金',width:100,align:"center"}, 
	        {field:'status',title:'租赁状态',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return typeToName(value,leaseType);
	        	}
	        }, 
	        {field:'leaseCount',title:'租赁数量',width:100,align:"center"}, 
	        {field:'leaseTime',title:'租赁时间',width:100,align:"center"}, 
	        {field:'returnTime',title:'归还时间',width:100,align:"center"}, 
	        {field:'operation',title:'操作',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return " <a  href='#' onclick=editRow("+index+");  >编辑</a> "+
	        		" <a  href='#' onclick=leaseRow("+index+");  >归还</a> "+
	                  " <a  href='#' onclick=deleteRow('"+row.leaseId+"'); >删除</a> ";
	        	}
	        	
	        },
	    ]]  
	
	});
	
	$("#addDialog").dialog({
		title: '新增租赁信息',  
	    width: 330,  
	    height:440,  
	    closed: true,  
	    cache: false,    
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Lease/addLease.json",
					type:"post",
					data:JSON.stringify({
						'userName':$("#adduserName").val(),
						'idCard':$("#addidCard").val(),
						'clothingName':$("#addclothingName").val(),
						'clothingType':$("#addclothingType").combobox("getValue"),
						'clothingRent':$("#addclothingRent").val(),
						'foregift':$("#addforegift").val(),
						'status':"",
						'leaseCount':$("#addleaseCount").val(),
						'leaseTime':$("#addleaseTime").datebox("getValue"),
						'returnTime':$("#addreturnTime").datebox("getValue")
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "新增成功！",'info'); 
						}else{
							$.messager.alert("信息", "新增失败！",'info'); 
						}
						loadData();
						$("#restoreDatagrid").datagrid("loadData",dataJson);
						$("#addDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#addDialog").dialog("close");
			}
		}]
	});
	
	$("#editDialog").dialog({
		title: '编辑租赁信息',  
	    width: 310,  
	    height:330,  
	    closed: true,  
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Lease/editLeaseById.json",
					type:"post",
					data:JSON.stringify({
						'leaseId':$("#editleaseId").val(),
						'clothingId':$("#editclothingId").val(),
						'userId':$("#edituserId").val(),
						'clothingRent':$("#editclothingRent").val(),
						'foregift':$("#editforegift").val(),
						'status':$("#editstatus").combobox("getValue"),
						'leaseCount':$("#editleaseCount").val(),
						'leaseTime':$("#editleaseTime").datebox("getValue"),
						'returnTime':$("#editreturnTime").datebox("getValue")
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "修改成功！",'info'); 
						}else{
							$.messager.alert("信息", "修改失败！",'info'); 
						}
						loadData();
						$("#restoreDatagrid").datagrid("loadData",dataJson);
						$("#editDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#editDialog").dialog("close");
			}
		}]
	})
	$("#leaseDialog").dialog({
		title: '归还服装',  
	    width: 310,  
	    height:160,  
	    closed: true,  
	    cache: false,    
	    modal: true ,
	    buttons:[{
			text:'确定',
			handler:function(){
				$('#restoreDatagrid').datagrid("selectRow",ind);
				var row = $('#restoreDatagrid').datagrid("getSelected");
				if(!row){
					$('#restoreDatagrid').datagrid("selectRow",ind);
					row = $('#restoreDatagrid').datagrid("getSelected");
				}
				
				if(row.leaseCount < $("#leaseCount").val()){
					$.messager.alert("信息", "归还数量不能大于租赁数量！",'info'); 
					return;
				}
				
				$.ajax({
					url:$WEB_ROOT_PATH+"/Return/addReturn.json",
					type:"post",
					data:JSON.stringify({
						'leaseId':row.leaseId,
						'leaseCount':$("#leaseCount").val(),
						'returnTime':$("#toReturnTime").datebox("getValue")
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "归还成功！",'info'); 
						}else{
							$.messager.alert("信息", "归还失败！",'info'); 
						}
						loadData();
						$("#restoreDatagrid").datagrid("loadData",dataJson);
						$("#leaseDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#leaseDialog").dialog("close");
			}
		}]
	});
})
function loadData(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Lease/getAllLease.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"clothingName":'',"clothingType":'',"returnTime":'',"leaseTime":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.leaseInfoList;
		}
	})
}
function doAdd(){
	$("#addDialog").dialog("center");
	$("#addDialog").dialog("open");
	$("#addDialogForm").form("clear");
}

function editRow(index){
	$("#editDialog").dialog("center");
	$("#editDialog").dialog("open");

	$('#restoreDatagrid').datagrid("selectRow",index);
	var row = $('#restoreDatagrid').datagrid("getSelected");
	if(!row){
		$('#restoreDatagrid').datagrid("selectRow",index);
		row = $('#restoreDatagrid').datagrid("getSelected");
	}
	
	$("#editleaseId").val(row.leaseId);
	$("#edituserId").val(row.userId);
	$("#editclothingId").val(row.clothingId);
	$("#editleaseCount").val(row.leaseCount);
	$("#editclothingRent").val(row.clothingRent);
	$("#editstatus").combobox("setValue",row.status);
	$("#editforegift").val(row.foregift);
	$("#editleaseTime").datebox("setValue",row.leaseTime);
	$("#editreturnTime").datebox("setValue",row.returnTime);
}

function deleteRow(id){
	 $.messager.confirm("操作提示", "您确定要删除吗？", function (data) {  
         if (data) {  
			$.ajax({
				url:$WEB_ROOT_PATH+"/Lease/deleteLeaseById.json",
				type:"post",
				async: false, 
				data: JSON.stringify({"leaseId":id}),
				contentType: "application/json",
				dataType:"json",
				success:function(result){
					if(result.data.success == "0"){
						$.messager.alert("信息", "删除成功！",'info'); 
					}else{
						$.messager.alert("信息", "删除失败！",'info'); 
					}
					loadData();
					$("#restoreDatagrid").datagrid("loadData",dataJson);
				}
			})
         } else {  
 		    return;  
 		 } 
	 })
}

function leaseRow(index){
	$("#leaseDialog").dialog("open");
	$("#leaseDialog").dialog("center");
	$("#leaseDialogForm").form("clear");
	ind = index;
}

function doSearch(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Lease/getAllLease.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"clothingName":$("#clothingName").val(),"clothingType":$("#clothingType").combobox("getValue"),"returnTime":$("#returnTime").datebox("getValue"),"leaseTime":$("#leaseTime").datebox("getValue")}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			if(result.data.leaseInfoList == undefined && result.data.leaseInfoList==null){
				dataJson = [];
			}else{
				dataJson = result.data.leaseInfoList;
			}
			$("#restoreDatagrid").datagrid("loadData",dataJson);
		}
	})
}